package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.service.CategoryBrandRelationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.CategoryDao;
import com.atguigu.gulimail.product.entity.CategoryEntity;
import com.atguigu.gulimail.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
@Resource
private CategoryBrandRelationService service;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    //查询所有分类以父子分类方式组织
    public List<CategoryEntity> listWithTree() {
        //1.查出所有分类
        List<CategoryEntity> list = baseMapper.selectList(null);

        //2.组织所有父子树形结构
        //找到所有一级分类
        List<CategoryEntity> level1Menus = list.stream().filter((categoryEntity) ->
        {
            return categoryEntity.getParentCid() == 0;
        }).map((menu)->//过滤设置每个一级分类中的子菜单
        {
            menu.setChildren(getChildrens(menu,list));
            return menu;
        }).sorted((menu1,menu2)->{//排序
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return level1Menus;
    }

    //批量删除菜单
    public void removeMenuByIds(List<Long> asList) {
        //TODO 1.检查当前菜单是否被别的地方引用
        //逻辑删除
      baseMapper.deleteBatchIds(asList);
    }
       //按照层级关系找到catelogId完整路径   2/25/225
    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths=new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId,paths);
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }
/*级联更新所有关联的数据*/
    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
service.updateCategory(category.getCatId(),category.getName());
    }

    //递归得到  255/25/5
    private List<Long> findParentPath(Long catelogId,  List<Long> paths){
            //寻找父id并收集放入list
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if (byId.getParentCid()!=0){
            findParentPath(byId.getParentCid(),paths);
        }
        return paths;
    }

    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        //所有一级菜单root在所有菜单all中寻找子菜单
        List<CategoryEntity> children = all.stream().filter((categoryEntity -> {//找到二级分类
            return categoryEntity.getParentCid() == root.getCatId();
        })).map(categoryEntity -> {//将所有的二级分类再次得到三级分类
            categoryEntity.setChildren(getChildrens(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{//排序
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());}
            ).collect(Collectors.toList());
        return children;
    }

}